package com.github.caijh.mall.config;

import javax.inject.Inject;

import com.github.caijh.mall.common.validator.AdminTokenValidator;
import com.github.caijh.mall.common.validator.FrontTokenValidator;
import com.github.caijh.mall.filter.ResponseFilter;
import com.github.caijh.mall.interceptor.AdminAuthInterceptor;
import com.github.caijh.mall.interceptor.AdminTokenInterceptor;
import com.github.caijh.mall.interceptor.FrontTokenInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Inject
    private AdminTokenValidator adminTokenValidator;
    @Inject
    private FrontTokenValidator frontTokenValidator;

    @Bean
    public HandlerInterceptor adminAuthInterceptor() {
        return new AdminAuthInterceptor();
    }

    @Bean
    public HandlerInterceptor adminTokenInterceptor(AdminTokenValidator adminTokenValidator) {
        return new AdminTokenInterceptor(adminTokenValidator);
    }

    @Bean
    public HandlerInterceptor frontTokenInterceptor(FrontTokenValidator frontTokenValidator) {
        return new FrontTokenInterceptor(frontTokenValidator);
    }

    public void addInterceptors(InterceptorRegistry registry) {
        //添加token拦截器
        //addPathPatterns添加需要拦截的命名空间；
        //excludePathPatterns添加排除拦截命名空间


        //后台token拦截
        registry.addInterceptor(adminTokenInterceptor(adminTokenValidator))
                .addPathPatterns("/api/admin/**")
                .excludePathPatterns("/api/admin/validate/**")
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/admin/logout")
                .excludePathPatterns("/api/admin/getLoginPic")
                .excludePathPatterns("/api/admin/wechat/config")
                .excludePathPatterns("/api/admin/authorize/login")
                .excludePathPatterns("/api/admin/payment/callback/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");

        //后台权限规则
        registry.addInterceptor(adminAuthInterceptor())
                .addPathPatterns("/api/admin/**")
                .excludePathPatterns("/api/admin/validate/**")
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/admin/logout")
                .excludePathPatterns("/api/admin/getLoginPic")
                .excludePathPatterns("/api/admin/payment/callback/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");


        //前端用户登录token
        registry.addInterceptor(frontTokenInterceptor(frontTokenValidator))
                .addPathPatterns("/api/front/**")
                .excludePathPatterns("/api/front/qrcode/**")
                .excludePathPatterns("/api/front/login/mobile")
                .excludePathPatterns("/api/front/login")
                .excludePathPatterns("/api/front/sendCode")
                .excludePathPatterns("/api/front/wechat/**")
                .excludePathPatterns("/api/front/search/keyword")
                .excludePathPatterns("/api/front/share")
                .excludePathPatterns("/api/front/article/**")
                .excludePathPatterns("/api/front/city/**")
                .excludePathPatterns("/api/front/product/hot")
                .excludePathPatterns("/api/front/products/**")
                .excludePathPatterns("/api/front/reply/**")
                .excludePathPatterns("/api/front/user/service/**")
                .excludePathPatterns("/api/front/logistics")
                .excludePathPatterns("/api/front/groom/list/**")
                .excludePathPatterns("/api/front/index")
                .excludePathPatterns("/api/front/category")
                .excludePathPatterns("/api/front/seckill/*")
                .excludePathPatterns("/api/front/seckill/list/*")
                .excludePathPatterns("/api/front/seckill/detail/*")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public FilterRegistrationBean<ResponseFilter> filterRegister(ResponseFilter responseFilter) {
        FilterRegistrationBean<ResponseFilter> registration = new FilterRegistrationBean<>(responseFilter);
        registration.addUrlPatterns("/*");
        return registration;
    }

}
